package com.platform.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ReUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.platform.entity.ActivityEntity;
import com.platform.service.ActivityService;
import com.platform.utils.PageUtils;
import com.platform.utils.Query;
import com.platform.utils.R;

import javax.annotation.Resource;

/**
 * 春芽活动表Controller
 *
 * @author czj
 * @email 729618941@qq.com
 * @date 2018-11-02 15:43:06
 */
@RestController
@RequestMapping("activity")
public class ActivityController {
    @Autowired
    private ActivityService activityService;


    /**
     * 查看列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("activity:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ActivityEntity> activityList = activityService.queryList(query);

        if (activityList.size() > 0) {
            for (ActivityEntity activityEntity : activityList) {
                activityEntity.setWxAppLink("../eventsDetail/eventsDetail?activityId=" + activityEntity.getId());
            }
        }

        int total = activityService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(activityList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("activity:info")
    public R info(@PathVariable("id") Integer id) {
        ActivityEntity activity = activityService.queryObject(id);

        return R.ok().put("activity", activity);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("activity:save")
    public R save(@RequestBody ActivityEntity activity) {

        //todo 开始时间和结束时间的校验
        Date activityStartTime = activity.getActivityStartTime();
        Date activityEndTime = activity.getActivityEndTime();
        if (activityStartTime == null) {
            return R.error("开始时间不能为空");
        }

        if (activityEndTime == null) {
            return R.error("结束时间不能为空");
        }


        if (DateUtil.isExpired(activityStartTime, DateField.SECOND, 0, activityEndTime)) {
            return R.error("结束时间 必须大于 开始时间");
        }

        if (DateUtil.isExpired(new Date(), DateField.SECOND, 0, activityEndTime)) {
            return R.error("结束时间 必须大于 当前时间");
        }


        //todo 纬度的校验
        if (!ReUtil.isMatch("((?:[0-9]|[1-8][0-9])\\.([0-9]{0,6}))|((?:90)\\.([0]{0,6}))", activity.getLatitude())) {
            return R.error("请输入正确的纬度，0-90.000000范围内");
        }

        //todo 经度的校验
        if (!ReUtil.isMatch("((?:[0-9]|[1-9][0-9]|1[0-7][0-9])\\.([0-9]{0,6}))|((?:180)\\.([0]{0,6}))", activity.getLongitude())) {
            return R.error("请输入正确的经度，0-180.000000范围内");
        }

        //todo 标签的校验
        String tags = activity.getTags();
        if (tags.contains("，")) {
            return R.error("标签不能含有中文逗号");
        }

        activity.setAddTime(new Date());
        activityService.save(activity);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("activity:update")
    public R update(@RequestBody ActivityEntity activity) {


        //todo 开始时间和结束时间的校验
        Date activityStartTime = activity.getActivityStartTime();
        Date activityEndTime = activity.getActivityEndTime();
        if (activityStartTime == null) {
            return R.error("开始时间不能为空");
        }
        if (activityEndTime == null) {
            return R.error("结束时间不能为空");
        }
        if (DateUtil.isExpired(activityStartTime, DateField.SECOND, 0, activityEndTime)) {
            return R.error("结束时间 必须大于 开始时间");
        }
        if (DateUtil.isExpired(new Date(), DateField.SECOND, 0, activityEndTime)) {
            return R.error("结束时间 必须大于 当前时间");
        }

        //todo 纬度的校验
        if (!ReUtil.isMatch("((?:[0-9]|[1-8][0-9])\\.([0-9]{0,6}))|((?:90)\\.([0]{0,6}))", activity.getLatitude())) {
            return R.error("请输入正确的纬度，0-90.000000范围内");
        }

        //todo 经度的校验
        if (!ReUtil.isMatch("((?:[0-9]|[1-9][0-9]|1[0-7][0-9])\\.([0-9]{0,6}))|((?:180)\\.([0]{0,6}))", activity.getLongitude())) {
            return R.error("请输入正确的经度，0-180.000000范围内");
        }

        //todo 标签的校验
        String tags = activity.getTags();
        if (tags.contains("，")) {
            return R.error("标签不能含有中文逗号");
        }

        activity.setUpdateTime(new Date());
        activityService.update(activity);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("activity:delete")
    public R delete(@RequestBody Integer[] ids) {
        activityService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ActivityEntity> list = activityService.queryList(params);

        return R.ok().put("list", list);
    }
}
